package com.klun.project.common.constans.entity;

public class TreeNode {
	public int val;
	public TreeNode left;
	public TreeNode right;

	public TreeNode(int value, TreeNode left, TreeNode right) {
		this.val = value;
		this.left = left;
		this.right = right;
	}

	public void add(TreeNode node) {
		if (null == node) {
			return;
		}
		if (node.val > this.val) {
			if (null == this.right) {
				this.right = new TreeNode(node.val);
			} else {
				this.right.add(new TreeNode(node.val));
			}
		} else if (node.val < this.val) {
			if (null == this.left) {
				this.left = new TreeNode(node.val);
			} else {
				this.left.add(new TreeNode(node.val));
			}
		}
		add(node.left);
		add(node.right);
	}

	public void add(Integer val) {
		if (null == val) {
			return;
		}
		if (val > this.val) {
			if (null == this.right) {
				this.right = new TreeNode(val);
			} else {
				this.right.add(new TreeNode(val));
			}
		} else if (val < this.val) {
			if (null == this.left) {
				this.left = new TreeNode(val);
			} else {
				this.left.add(new TreeNode(val));
			}
		}
	}

	public void preOrderTraveral(TreeNode node) {
		if (node == null) {
			return;
		}
		System.out.print(node.val + " , ");
		preOrderTraveral(node.left);
		preOrderTraveral(node.right);
	}

	public void inOrderTraveral(TreeNode node) {
		if (node == null) {
			return;
		}
		int ans = node.val;
		System.out.print(ans + " , ");
		inOrderTraveral(node.left);
		inOrderTraveral(node.right);
	}


	public void postOrderTraveral(TreeNode node) {
		if (node == null) {
			return;
		}
		postOrderTraveral(node.left);
		postOrderTraveral(node.right);
		System.out.print(node.val + " , ");
	}


	public TreeNode(int value) {
		this.val = value;
	}

	public TreeNode() {
	}

	public int getVal() {
		return val;
	}

	public void setVal(int val) {
		this.val = val;
	}

	public TreeNode getLeft() {
		return left;
	}

	public void setLeft(TreeNode left) {
		this.left = left;
	}

	public TreeNode getRight() {
		return right;
	}

	public void setRight(TreeNode right) {
		this.right = right;
	}

}
